#!/usr/bin/perl
use Getopt::Std;
getopt("d:t",\%args);
my $dir = $args{d};
my $temp_d = $args{t};
chomp($dir);
if($dir eq "")
{
        die "entered dir name where subfolders are created is empty\n";
}
if($temp_d eq "")
{
        die "entered temp dir name where subfolders are created is empty\n";
}


#$dir = '/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/programtest';
#$dir = '/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_vte/clean/beagle';
#$i=0;
$dic = $dir;
#$dir = "$dir/temp/$temp_d";
$dir = $temp_d;
system("gunzip $dir/1/1/*.gz");
system("head -2 $dir/1/1/beagle_out_1.geno_segment1.bgl.gz.phased >$dir/whole_geno_phased");
system("rm $dir/whole_r2");
system("rm $dir/whole_no_snp_hapmap.txt");
system("head -1 $dir/1/1/beagle_out_1.geno_segment1.bgl.gz.gprobs > $dir/whole_gprobs");
for($i1=0;$i1<22;$i1++)
{
	$j = $i1+1;
	$tdir = "$dir/$j";
	system("ls -l $tdir >$dir/ls_l_check");
	#grabing all the directory names(numbers) in to the file
	$tm ="ls -p $tdir| grep \"/\"|/usr/bin/perl -e '".'while($l=<STDIN>) {chomp $l;$l =~ s/\///g; if($l =~ m/\d/) {print $l."\n"; }}'."' > $tdir/temp_num";
	#print $tm."\n";
	system($tm);
	open BUFF,"<$tdir/temp_num" or die "Can't open $tdir/temp_num : $!";
	@{"array".$j} = <BUFF>;
	system("rm $tdir/temp_num");
	$line = join("",@{"array".$j});
	print "Chr : $j\t Sub dir:  $line\n";
	@{"array".$j} = split("\n",$line);
	@{"array".$j} = sort {$a <=> $b} @{"array".$j};

	#based on the log file exteriors (buffers) are trimmed
	open BUFF,"<$tdir/perl_chop_beagle_files.log" or die "Can't open perl_chop_beagle_files.log : $!";
	$ni = 0;
	while($line = <BUFF>)
	{
		chomp $line;
		$line =~ s/\n//g;
		$line =~ s/\r//g;
		if($line =~ m/Segment Min/ )
		{
		#	print "$line\n";
			@array = split(":",$line);
			$array[1] =~ s/\t//g;
			$array[1] =~ s/\s//g;
			#print $array[1]."\n";
			${"array1".$j}[$ni] = $array[1];
			$ni++;
		}
	}
	open BUFF,"<$tdir/perl_chop_beagle_files.log" or die "Can't open perl_chop_beagle_files.log : $!";
        $ni = 0;
        while($line = <BUFF>)
        {
                chomp $line;
                $line =~ s/\n//g;
                $line =~ s/\r//g;
                if($line =~ m/Segment Max/ )
                {
                        @array = split(":",$line);
                        $array[1] =~ s/\t//g;
                        $array[1] =~ s/\s//g;
			#print $array[1]."\n";
                        ${"array2".$j}[$ni] = $array[1];
                        $ni++;
                }
        }
	if(@{"array1".$j} ne @{"array".$j} || @{"array2".$j} ne @{"array".$j})
	{
		die "length of array1.$j and array.$j not equal";
	}
	#$i =1;
	#open(KK,"$dic/temp/failed.txt");	
	for($i=0;$i<@{"array".$j};$i++)
	{
		$temp1 = ${"array".$j}[$i];
		$temp2 = ${"array1".$j}[$i];
		if($i == @{"array2".$j}-1)
		{
			$temp3 = ${"array2".$j}[$i]+1;
		}
		else
		{
			$temp3 = ${"array2".$j}[$i];
		}
		system("/bin/gunzip $dir/$j/$temp1/*.gz");
		$temp4 = "/usr/bin/perl $dic/bin/perl_exclude_snps_buffer.pl -m $dir/$j/markers.txt -i $dir/$j/$temp1/beagle_out_$temp1.geno_segment$temp1.bgl.gz.phased -u $temp3 -l $temp2";
		$temp5 = "/usr/bin/perl $dic/bin/perl_exclude_snps_buffer_r2.pl -m $dir/$j/markers.txt -i $dir/$j/$temp1/beagle_out_$temp1.geno_segment$temp1.bgl.gz.r2 -u $temp3 -l $temp2";
		$temp6 = "/usr/bin/perl $dic/bin/perl_exclude_snps_buffer_gprobs.pl -m $dir/$j/markers.txt -i $dir/$j/$temp1/beagle_out_$temp1.geno_segment$temp1.bgl.gz.gprobs -u $temp3 -l $temp2 -f $dir/failed.txt";
	print $temp6."\n";
		system($temp4);
		system($temp5);
		system($temp6);
	
		system("cat $dir/$j/$temp1/beagle_out_$temp1.geno_segment$temp1.bgl.gz.phased_striped >> $dir/whole_geno_phased");
		system("wc -l $dir/$j/$temp1/beagle_out_$temp1.geno_segment$temp1.bgl.gz.phased_striped");
		system("cat $dir/$j/$temp1/beagle_out_$temp1.geno_segment$temp1.bgl.gz.r2_striped >> $dir/whole_r2");
		system("cat $dir/$j/$temp1/beagle_out_$temp1.geno_segment$temp1.bgl.gz.gprobs_striped >> $dir/whole_gprobs");
		system("wc -l $dir/$j/$temp1/beagle_out_$temp1.geno_segment$temp1.bgl.gz.gprobs_striped");
	}
		system("cat $dir/$j/no_snp_hapmap.txt >> $dir/whole_no_snp_hapmap.txt");	
	 
}
